package com.example.controller;


import com.alibaba.druid.sql.repository.function.SignatureUtils;
import com.example.utils.SignUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhexueqi
 * @ClassName WxController
 * @since 2025/3/2    0:56
 */
@RestController
@Api(tags = "微信相关接口")
@Slf4j
public class WxController {

    @GetMapping("/wxverify")
    public String weixin(HttpServletRequest request, HttpServletResponse response){
        String echostr = null;
        //token验证代码段
        try{
            log.info("请求已到达，开始校验token");
            //这里对应的文档里面的几个参数，如果不清楚，请查看文档
            if (StringUtils.isNotBlank(request.getParameter("signature"))) {
                String signature = request.getParameter("signature");
                String timestamp = request.getParameter("timestamp");
                String nonce = request.getParameter("nonce");
                echostr = request.getParameter("echostr");
                log.info("signature[{}], timestamp[{}], nonce[{}], echostr[{}]", signature, timestamp, nonce, echostr);
//                if (SignatureUtilscheckSignature(signature, timestamp, nonce)) {
                    log.info("数据源为微信后台，将echostr[{}]返回！", echostr);
                    response.getOutputStream().println(echostr);
                    return echostr;
//                }
            }
        }catch (IOException e){
            log.error("校验出错");
            e.printStackTrace();
        }
        return echostr;
    }

}
